<?php

/**
 * Description of Representation
 *
 * @author ngo
 */
class Db_Table_Representation extends Ngo_Db_Table_Abstract {

    public function __construct($config = array()) {
        $this->_name = 'spectacle_has_representation';
        parent::__construct($config);
    }

    /**
     * Retourne la liste des n prochaine représentation prévu 
     * avec l'ensemble des données nécessaire pour l'affichage
     * 
     * @param int $limit
     * @return Zend_Db_Table_Rowset
     */
    public function getNextRepresentation($limit = -1) {
        $db = $this->getAdapter();
        $select = $db->select();

        $select->from(array('shr' => 'spectacle_has_representation'),
                        array('id_spectacle_has_representation',
                            'id_spectacle',
                            'id_lieu',
                            'representation_date'=>'date',
                            'representation_time'=>'time'))
                ->join(array('s' => 'spectacle'), 
                        'shr.id_spectacle = s.id_spectacle', 
                        array('spectacle_nom'=>'nom', 
                            'spectacle_descriptif'=>'descriptif', 
                            'spectacle_pathimg'=>'path_img'))
                ->join(array('l' => 'lieu'), 
                        'shr.id_lieu = l.id_lieu', 
                        array('lieu_name'=>'name',
                            'lieu_adress'=>'adress', 
                            'lien_gmap'=>'gmap_link',
                            'seat_maxavailable'=>'seat_available'))
                ->join(array('srr' => 'spectater_reserve_representation'),
                        'shr.id_spectacle_has_representation = srr.id_spectacle_has_representation',
                        array('seat_reserved'=>'sum(number_seat)'))
                ->where('shr.date > CURDATE()')
                ->group('shr.id_spectacle_has_representation')
                ->order(array('shr.date ASC','shr.time ASC'));


        if ((int) $limit >= 0)
            $select->limit((int) $limit);

        $stmt = $db->query($select);
        return $stmt->fetchAll();
    }

}
